﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Model;
using JYStockAnalyser.Face;
using JYStockAnalyser.ClientLib;
using ZedGraph;

namespace JYStockAnalyser.WinUI
{
    /// <summary>
    /// 多指标分析界面
    /// </summary>
    public partial class FrmMultipleGuideLineAnalysecs : FrmFunctionBase
    {
        public FrmMultipleGuideLineAnalysecs()
        {
            InitializeComponent();         
        }

        private void FrmMultipleGuideLineAnalysecs_Load(object sender, EventArgs e)
        {
            this.stockInputer1.BindData();

            StyleManager.SetStyle(this.zedGraphControl1);
            this.zedGraphControl1.GraphPane.Legend.Position = LegendPos.Top;
            this.zedGraphControl1.GraphPane.Legend.FontSpec.Size = 12F;
            this.zedGraphControl1.GraphPane.Legend.IsShowLegendSymbols = true;

            this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.FontColor = Color.White;
            this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 12F;
            this.zedGraphControl1.GraphPane.Y2Axis.Scale.FontSpec.FontColor = Color.White;
            this.zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Size = 12F;
        }

        private void btnSelectGuideLines_Click(object sender, EventArgs e)
        {
            FrmSelectGuideLine frm = new FrmSelectGuideLine();
            if (this.lblSelectedGuideLines.Tag != null)
            {
                frm.SelectedGuideLines.AddRange((List<GuideLineType>)this.lblSelectedGuideLines.Tag);
            }
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.lblSelectedGuideLines.Tag = frm.SelectedGuideLines;
                this.lblSelectedGuideLines.Text = "";
                foreach (GuideLineType line in frm.SelectedGuideLines)
                {
                    this.lblSelectedGuideLines.Text += (line.Name + "、");
                }

                if (this.lblSelectedGuideLines.Text.Length != 0)
                {
                    this.lblSelectedGuideLines.Text = this.lblSelectedGuideLines.Text.Remove(this.lblSelectedGuideLines.Text.Length - 1, 1);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            m_doSearch();
        }

        private void m_doSearch()
        {
            if (this.stockInputer1.SelectedStockNO == "")
            {
                Tool.ShowInformationBox("请输入股票代码!");
                this.stockInputer1.Focus();
                return;
            }

            if (this.lblSelectedGuideLines.Text.Length == 0)
            {
                Tool.ShowInformationBox("请选择一个以上的指标!");
                this.btnSelectGuideLines.Focus();
                return;
            }

            string stockNO = this.stockInputer1.SelectedStockNO;
            string analyseTimeType = this.periodInputer1.AnalyseTimeType;
            YearQuarter beginYearQuarter = this.periodInputer1.BeginQuarter;
            YearQuarter endYearQuarter = this.periodInputer1.EndQuarter;

            List<GuideLineType> lines = (List<GuideLineType>)this.lblSelectedGuideLines.Tag;       

            object[] objs = new StockManageFace().SearchGuideLines(stockNO, analyseTimeType, beginYearQuarter, endYearQuarter, lines.ToArray());


            Stock stock = (Stock)objs[0];

            if (stock == null)
            {
                Tool.ShowInformationBox("没有此股票代码!");
                this.stockInputer1.Focus();
                return;
            }

            GraphPane myPane = this.zedGraphControl1.GraphPane;
            myPane.CurveList.Clear();

            for (int i = 2; i < objs.Length; i++)
            {
                List<GuideLine> searchedlines = (List<GuideLine>)objs[i];
                GuideLinesCollator.CollateLines(analyseTimeType, beginYearQuarter, endYearQuarter, lines[0], searchedlines);


                if (i == 2)
                {
                    List<string> strs = new List<string>();
                    foreach (GuideLine line in searchedlines)
                    {
                        strs.Add(line.GetTimeSimpleString());
                    }

                    myPane.XAxis.Scale.TextLabels = strs.ToArray();
                }

                List<double> values = new List<double>();

                foreach (GuideLine line in searchedlines)
                {
                    values.Add((double)line.Value);
                }

                myPane.XAxis.Type = AxisType.Text;

                LineItem myCurve = myPane.AddCurve(lines[i - 2].GetFullName(), null, values.ToArray(), Color.Yellow);
                StyleManager.SetCommonLineItemStyle(myCurve);
                myCurve.Line.Color = StyleManager.GetLineColor(i - 2);
                myCurve.Symbol.Fill = new Fill(StyleManager.GetSymbolColor(i - 2));
                myCurve.Symbol.Border.Color = myCurve.Line.Color;

                this.zedGraphControl1.AxisChange();
                zedGraphControl1.Refresh();
            }
        }

        private void btnFindStockByTrade_Click(object sender, EventArgs e)
        {
            FrmSelectStocks frm = new FrmSelectStocks();
            frm.StockSelected += new FrmSelectStocks.StockSelectedHandler(frm_StockSelected);
            frm.Show();
        }

        void frm_StockSelected(Stock stock)
        {
            this.stockInputer1.SelectedItem = stock;
            m_doSearch();
        }
    }
}
